import { ref, onMounted } from 'vue'
import { onBeforeRouteUpdate, useRoute } from 'vue-router'
import { getCategoryAPI } from '@/apis/category'


export function useCategory() {
    //获取分类名字
const categoryData = ref({})
const route = useRoute()
const getCategory = async (id = route.params.id) => {
  const res = await getCategoryAPI(id)
  categoryData.value = res.result
} 
//如果路由更新了 就接受接受最新路由的参数 没有就是默认路由的参数

onMounted(() => {
  getCategory()
})

//路由参数变化时 获取分类名字
onBeforeRouteUpdate((to) => {
  getCategory(to.params.id) //to是最新的路由参数
})

return {
  categoryData
}

}
